﻿using System;
using System.Collections.Generic;
using System.Text;
using Store.Model;
using System.Linq;
using System.Threading.Tasks;
using Store.Model.Models;
using Store.ViewModel;

namespace Store.Dal
{
    public class HandlingOrderDal
    {
        //实例化上下文
        private readonly StoreContext db;
        public HandlingOrderDal(StoreContext _db)
        {
            db = _db;
        }

        //显示查询订单列表
        public List<OrderView> GetOrders(int sort, int state, string number, string user,
            string time, int page, int limit, out int total)
        {//三表联查放入视图模型
            var query = from o in db.Orders
                        join c in db.Commodities
                            on o.CommodityId equals c.CommodityId
                        join u in db.Users
                            on o.UserId equals u.UserId

                        select new OrderView
                        {
                            OrderId = o.OrderId,
                            OrderNumber = o.OrderNumber,
                            ArticleNumber = c.ArticleNumber,
                            OrderCreateTime = o.OrderCreateTime,
                            UserName = u.UserName,
                            UserPhone = u.UserPhone,
                            OrderPrice = c.CommodityPrice * o.OrderSum,
                            OrderPayment = o.OrderPayment,
                            OrderSource = o.OrderSource,
                            OrderState = o.OrderState
                        };

            if (sort != 0)
            {//排序
                if (sort == 1)
                    query = query.OrderByDescending(s => s.OrderPrice);
                else if (sort == 2)
                    query = query.OrderBy(s => s.OrderPrice);
                else if (sort == 3)
                    query = query.OrderByDescending(s => s.OrderCreateTime);
                else if (sort == 4)
                    query = query.OrderBy(s => s.OrderCreateTime);
                else
                    query = query.OrderBy(s => s.OrderId);
            }
            //状态查询
            if (state != 0)
            {
                query = query.Where(s => s.OrderState == state);
            }
            //文本框查询关键词-订单号、商品货号
            if (!string.IsNullOrEmpty(number))
            {
                query = query.Where(s => s.OrderNumber.Contains(number) || s.ArticleNumber.Contains(number));
            }
            //文本框查询收货人-用户昵称、手机号
            if (!string.IsNullOrEmpty(user))
            {
                query = query.Where(s => s.UserName.Contains(user) || s.UserPhone.Contains(user));
            }
            //下拉框查询提交事件
            if (!string.IsNullOrEmpty(time))
            {
                query = query.Where(s => s.OrderCreateTime == Convert.ToDateTime(time));
            }
            //获取总条数
            total = query.Count();
            //db.Configuration.ProxyCreationEnabled = false;
            //分页
            var list = query.Skip((page - 1) * limit)
                            .Take(limit)
                            .ToList();
            //传参
            return list;
        }

        //显示订单信息
        public List<OrderInfo> GetOrder(int id)
        {
            var query = from o in db.Orders
                        join u in db.Users
                        on o.UserId equals u.UserId
                        where o.OrderId == id
                        select new OrderInfo
                        {
                            OrderId = o.OrderId,
                            OrderNumber = o.OrderNumber,
                            OrderState = o.OrderState,
                            OrderPayment = o.OrderPayment,
                            UserAccount = u.UserAccount,
                            OrderCreateTime = o.OrderCreateTime,
                            OrderSource = o.OrderSource
                        };
            return query.ToList();
        }

        //显示收货人信息
        public List<UserInfo> GetUserInfo(int id)
        {
            var query = from u in db.Users
                        join a in db.Addresses
                        on u.UserId equals a.UserId
                        join o in db.Orders
                        on u.UserId equals o.UserId
                        where o.OrderId == id
                        select new UserInfo
                        {
                            OrderId = o.OrderId,
                            UserId = u.UserId,
                            UserName = u.UserName,
                            UserPhone = u.UserPhone,
                            AddressName = a.AddressName,
                            AddressPostal = a.AddressPostal
                        };
            return query.Distinct().ToList();
        }

        //显示商品信息
        public List<CommodityInfo> GetCommodity(int id)
        {
            var query = from c in db.Commodities
                        join o in db.Orders
                        on c.CommodityId equals o.CommodityId
                        join t in db.Typees
                        on c.TypeId equals t.TypeId
                        where o.OrderId == id
                        select new CommodityInfo
                        {
                            OrderId = o.OrderId,
                            CommodityId = c.CommodityId,
                            CommodityName = c.CommodityName,
                            CommodityImg = c.CommodityImg,
                            CommodityPrice = c.CommodityPrice,
                            Describe = t.Describe,
                            OrderSum = o.OrderSum,
                            Sum = (decimal)(c.CommodityPrice * o.OrderSum),
                        };
            return query.ToList();
        }

        //显示费用合计
        public List<CostView> GetCostViews(int id)
        {
            var query = from c in db.Commodities
                        join p in db.Promotions
                        on c.PromotionId equals p.PromotionId
                        join o in db.Orders
                        on c.CommodityId equals o.CommodityId
                        where o.OrderId == id
                        select new CostView
                        {
                            OrderId = o.OrderId,
                            CommodityPrice = c.CommodityPrice,
                            OrderSum = o.OrderSum,
                            Sumorder = c.CommodityPrice * o.OrderSum,
                            Price = p.Price,
                            Sum = (decimal)(c.CommodityPrice * o.OrderSum - p.Price),
                        };
            return query.ToList();
        }

        //修改
        public int UpdateUser(int userid,string username,string userphone,string addressname,string addresspostal)//,Addresse addresse
        {
            var data = db.Users.Find(userid);
            //var data = from a in db.Users
            //            where a.UserId == userid
            //            select a;
            if (data != null)
            {
                data.UserName = username;
                data.UserPhone = userphone;
            }
            var data1 = db.Addresses.Find(userid);
            //var data1 = from a in db.Addresses
            //            where a.AddressId == userid
            //            select a;
            if (data1 != null)
            {
                data1.AddressName = addressname;
                data1.AddressPostal = addresspostal;

            }
            return db.SaveChanges();

        }
        ////修改用户地址和邮政编码
        //public int UpdateUserAddress(Addresse addresse)
        //{
        //    var data = db.Addresses.Find(addresse.AddressId);
        //    if (data != null)
        //    {
        //        data.AddressName = addresse.AddressName;
        //        data.AddressPostal = addresse.AddressPostal;
        //        return db.SaveChanges();
        //    }
        //    return -1;
        //}

        //添加订单
        public int AddOrder(Order orders)
        {
            db.Orders.Add(orders);
            return db.SaveChanges();
        }

        //删除订单
        public int DelOrder(int id)
        {
            var data = db.Orders.Find(id);
            if (data != null)
            {
                db.Orders.Remove(data);
                return db.SaveChanges();
            }
            return -1;
        }

        //批量删除
        public int DelOrders(string[] ids)
        {
            var query = from o in db.Orders
                        where ids.Contains(o.OrderId.ToString())
                        select o;
            if (query.Count() > 0)
            {
                db.Orders.RemoveRange(query.ToList());
                return db.SaveChanges();
            }
            return -1;
        }

        //修改订单状态
        public int UpdOrderState(int id, int state)
        {
            var data = db.Orders.Find(id);
            if (data != null)
            {
                data.OrderState = state;
                return db.SaveChanges();
            }
            return -1;
        }

        //批量确认订单状态
        public int UpdOrderStates(string[] ids)
        {
            var query = from o in db.Orders
                        where ids.Contains(o.OrderId.ToString())
                        select o;
            if (query.Count() > 0)
            {
                foreach (var item in query)
                {
                    if (item.OrderState == 6)
                    { 
                        item.OrderState = 2;
                    }
                    
                }
                return db.SaveChanges();
            }
            return -1;
        }

    }
}
